//
//  CustomButton.m
//  产业头条
//
//  Created by ZJ on 16/12/22.
//  Copyright © 2016年 ZJ. All rights reserved.

//

#import "CustomButton.h"
#import "ZJAdditions.h"

@interface CustomButton ()
{
    UIImage *_normalImage;
    UIImage *_selectedImage;
    NSString *_normalTitle;
    NSString *_selectedTitle;
    UIColor *_normalTitleColor;
    UIColor *_selectedTitleColor;
}
@end
@implementation CustomButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        self.selected = NO;
        //单击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_gesture:)];
        [self addGestureRecognizer:tapGesture];
        
    }
    return self;
}

#pragma mark - 点击事件
- (void)action_gesture:(UIGestureRecognizer *)sender{
    self.selected = !self.selected;
    if (self.delegate) {
        [self.delegate action_customButtonClick:self];
    }
}

#pragma mark - 赋值
- (void)setSelected:(BOOL)selected{
    _selected = selected;
//    if (!selected) {//CustomButtonStateNormal
//        self.imageView.image = _normalImage;
//        self.titleLabel.text = _normalTitle;
//        self.titleLabel.textColor = _normalTitleColor;
//        
//    }else{//CustomButtonStateSelected
//        self.imageView.image = _selectedImage;
//        self.titleLabel.text = _selectedTitle;
//        self.titleLabel.textColor = _selectedTitleColor;
//        if (_selectedTitle == nil) {
//          self.titleLabel.text = _normalTitle;
//        }
//    }
}


- (void)setImage:(UIImage *)image forState:(CustomButtonState)State{
    if (State == CustomButtonStateNormal) {
        _normalImage = image;
    }else if(State == CustomButtonStateSelected){
        _selectedImage = image;
    }
}

- (void)setTitle:(NSString *)title forState:(CustomButtonState)State{
    if (State == CustomButtonStateNormal) {
        _normalTitle = title;
    }else if(State == CustomButtonStateSelected){
        _selectedTitle = title;
    }
}


- (void)setTitleColor:(UIColor *)titleColor forState:(CustomButtonState)State{
    if (State == CustomButtonStateNormal) {
        _normalTitleColor = titleColor;
    }else if(State == CustomButtonStateSelected){
        _selectedTitleColor = titleColor;
    }
}





#pragma mark - Setter
#pragma mark - Getters
- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0 , self.height * 0.6 , self.height * 0.6)];
        _imageView.centerX = self.width / 2;
        _imageView.contentMode = UIViewContentModeCenter;
    }
    return _imageView;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.imageView.bottom, self.width, self.height * 0.35)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _titleLabel;
}

@end
